<template>
  <view class="content">
    <!-- 顶部导航 -->
    <Navbar></Navbar>
    <scroll-view
      scroll-y
      class="main"
      @scrolltolower="scrolltolower"
      refresher-enabled
      :refresher-triggered="refresherTriggered"
      @refresherrefresh="refresherrefresh"
    >
      <!-- 轮播图 -->
      <Carousel :banners="banners" height="280rpx"></Carousel>
      <!-- 前台分类 -->
      <CateScroll :categoryHeadMutli="categoryHeadMutli"></CateScroll>
      <!-- 人气推荐 -->
      <view class="panel recommend">
        <view class="item" v-for="item in hotMutli" :key="item.id">
          <view class="title">
            {{ item.title }}<text>{{ item.alt }}</text>
          </view>
          <navigator
            hover-class="none"
            :url="`/pages/recommend/index?type=${item.type}`"
            class="cards"
          >
            <image
              mode="aspectFit"
              v-for="img in item.pictures"
              :key="img"
              :src="img"
            ></image>
          </navigator>
        </view>
      </view>
      <!-- 新鲜好物 -->
      <view class="panel fresh">
        <view class="title">
          新鲜好物
          <navigator
            hover-class="none"
            class="more"
            url="/pages/recommend/index?type=5"
            >更多</navigator
          >
        </view>
        <view class="cards">
          <navigator
            hover-class="none"
            :url="`/pages/goods/index?id=${item.id}`"
            v-for="item in homeNew"
            :key="item.id"
          >
            <image mode="aspectFit" :src="item.picture"></image>
            <view class="name">{{ item.name }}</view>
            <view class="price">
              <text class="small">¥</text>{{ item.price }}
            </view>
          </navigator>
        </view>
      </view>
      <!-- 热门品牌 ，纯静态，无接口-->
      <view class="panel brands">
        <view class="title">
          热门品牌
          <navigator hover-class="none" class="more" url="/pages/list/index"
            >更多</navigator
          >
        </view>
        <view class="cards">
          <navigator hover-class="none" url="/pages/goods/index">
            <image
              mode="aspectFit"
              src="http://static.botue.com/erabbit/static/uploads/brand_logo_1.jpg"
            ></image>
            <view class="name">小米</view>
            <view class="price">99元起</view>
          </navigator>
          <navigator hover-class="none" url="/pages/goods/index">
            <image
              mode="aspectFit"
              src="http://static.botue.com/erabbit/static/uploads/brand_logo_2.jpg"
            ></image>
            <view class="name">TCL</view>
            <view class="price">199起</view>
          </navigator>
          <navigator hover-class="none" url="/pages/goods/index">
            <image
              mode="aspectFit"
              src="http://static.botue.com/erabbit/static/uploads/brand_logo_3.jpg"
            ></image>
            <view class="name">饭小宝</view>
            <view class="price">9.9起</view>
          </navigator>
          <navigator hover-class="none" url="/pages/goods/index">
            <image
              mode="aspectFit"
              src="http://static.botue.com/erabbit/static/uploads/brand_logo_4.jpg"
            ></image>
            <view class="name">鳄鱼</view>
            <view class="price">299起</view>
          </navigator>
        </view>
      </view>
      <!-- 猜你喜欢 -->
      <Guess
        :homeGoodsGuessLike="homeGoodsGuessLike"
        :homeGoodsGuessLikeTotal="homeGoodsGuessLikeTotal"
      ></Guess>
    </scroll-view>
  </view>
</template>

<script>
import {
  getHomeBanner,
  getCategorylist,
  getHotlist,
  getNewlist,
  getGuessLike
} from '@/api/home'
import Navbar from './components/Navbar.vue'
export default {
  data () {
    return {
      banners: [], //轮播图数据
      categoryHeadMutli: [], //前台分类数据
      hotMutli: [], //人气推荐数据
      homeNew: [], //新鲜好物
      homeGoodsGuessLike: [], //猜你喜欢数据
      pages: 1,
      homeGoodsGuessLikeTotal: 0, //总数量
      loading: false,
      refresherTriggered: false
    }
  },
  components: {
    Navbar
  },
  methods: {
    onLoad () {
      this.data = { page: 1, pageSize: 10 }
      /**
       * 调用获取轮播图方法
       */
      this.getHomeBanner()
      /**
       * 调用获取前台分类方法
       */
      this.getCategorylist()
      /**
       * 调用人气推荐方法
       */
      this.getHotlist()
      /**
       * 获取新鲜好物
       */
      this.getNewlist()
      /**
       * 获取猜你喜欢
       */
      this.getGuessLike()
    },
    /**
     * 获取轮播图数据
     */
    async getHomeBanner () {
      const { result } = await getHomeBanner()
      // console.log(result);
      this.banners = result
    },
    /**
     * 获取前台分类
     */
    async getCategorylist () {
      const { result } = await getCategorylist()
      // console.log(result);
      this.categoryHeadMutli = result
    },
    /**
     * 获取人气推荐
     */
    async getHotlist () {
      const { result } = await getHotlist()
      // console.log(result);
      this.hotMutli = result
    },
    /**
     * 获取新鲜好物
     */
    async getNewlist () {
      const { result } = await getNewlist()
      //  console.log(result);
      this.homeNew = result
    },
    /**
     * 获取猜你喜欢
     */
    async getGuessLike () {
      const res = await getGuessLike(this.data)
      // console.log(res)
      //  新数据追加到旧数据
      this.homeGoodsGuessLike = [
        ...this.homeGoodsGuessLike,
        ...res.result.items
      ]
      // 总页数
      this.pages = res.result.pages
      //  总数量
      this.homeGoodsGuessLikeTotal = res.result.counts
    },
    /**
     * 分页处理
     */
    async scrolltolower (e) {
      // console.log(e)
      if (this.loading) return
      // console.log(e);
      //1.查找api,绑定滚动到底部的事件，
      // 2.判断还有没有下一页，当前页数<后台的总页数
      //3.this.data.page++发送请求，新数据追加到旧数据
      if (this.data.page < this.pages) {
        this.data.page++
        this.loading = true
        await this.getGuessLike()
        this.loading = false
      }
    },
    /**
     * 下拉刷新，组件自带 refresherrefresh方法
     */
    refresherrefresh (e) {
      this.refresherTriggered = true
      // console.log(e);
      // console.time()
      this.banners = [] //轮播图数据
      this.categoryHeadMutli = [] //前台分类数据
      this.hotMutli = [] //人气推荐数据
      this.homeNew = [] //新鲜好物
      this.homeGoodsGuessLike = [] //猜你喜欢数据
      this.pages = 1
      this.loading = false
      this.data = { page: 1, pageSize: 10 }
      Promise.all([
        this.getHomeBanner(),
        this.getCategorylist(),
        this.getHotlist(),
        this.getNewlist(),
        this.getGuessLike()
      ]).then(() => {
        // console.timeEnd()
        this.refresherTriggered = false
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.content {
  height: 100vh;
  display: flex;
  flex-direction: column;
}
.main {
  flex: 1;
  overflow: hidden;
  background-color: #f7f7f8;
}
/* 公共面板 */
.panel {
  margin: 20rpx 20rpx 0;
  border-radius: 10rpx;
  background-color: #fff;
  .title {
    display: flex;
    align-items: center;
    padding: 24rpx 24rpx 0;
    font-size: 32rpx;
    color: #262626;
    position: relative;
    text {
      font-size: 24rpx;
      color: #7f7f7f;
      margin-left: 18rpx;
    }
  }
  .more {
    position: absolute;
    right: 24rpx;
    font-size: 26rpx;
    color: #7f7f7f;
  }
  .cards {
    display: flex;
  }
  .name {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}
/* 推荐专区 */
.recommend {
  display: flex;
  flex-wrap: wrap;
  .item {
    display: flex;
    flex-direction: column;
    width: 50%;
    height: 254rpx;
    border-right: 1rpx solid #eee;
    border-top: 1rpx solid #eee;
    &:nth-child(2n) {
      border-right: 0 none;
    }
    &:nth-child(-n + 2) {
      border-top: 0 none;
    }
  }
  .cards {
    flex: 1;
    padding: 15rpx 20rpx;
    justify-content: space-between;
    image {
      width: 48%;
    }
  }
}

/* 新鲜好物 */
.fresh {
  min-height: 320rpx;
  .price {
    line-height: 1;
    text-align: left;
    font-size: 26rpx;
    color: #cf4444;
  }
  .small {
    font-size: 80%;
  }
}
.fresh .cards navigator,
.brands .cards navigator {
  width: 25%;
  padding: 15rpx 20rpx 20rpx;
  line-height: 1;
  text-align: center;
}
.fresh image,
.brands image {
  width: 126rpx;
  height: 126rpx;
}
.fresh .name,
.brands .name {
  margin: 20rpx 0 16rpx;
  font-size: 24rpx;
  color: #262626;
}
.brands {
  .price {
    font-size: 24rpx;
    color: #999;
  }
}
</style>
